
#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}

/**
 *  遍历子控件
 */
- (void)logSubviews
{
    static void (^logSubviews)(UIView *);
    logSubviews = ^(UIView *view) {
        for (UIView *subview in view.subviews) {
            NSLog(@"%@", subview);
            logSubviews(subview);
        }
    };
    
    logSubviews(self);
}

// 清空一个控件及其内部所有子控件上的约束
- (void)clearAllConstraints
{
    for (UIView *subview in self.subviews) {
        [subview clearAllConstraints];
    }
    for (NSLayoutConstraint *constraint in self.constraints) {
        [self removeConstraint:constraint];
    }

}

// 清空一个控件内部所有子控件上的约束
- (void)clearAllConstraintsInSubviews
{
    for (UIView *subview in self.subviews) {
        [subview clearAllConstraintsInSubviews];
        for (NSLayoutConstraint *constraint in self.constraints) {
            [self removeConstraint:constraint];
        }
    }
    
}

/**
 *  获取键盘对象
 *
 *  @return 返回键盘对象
 */
+ (UIView *)getKeyboard
{
    __block UIView *keyboardView;
    
    static void (^findKeyboardInView)(UIView *);
    findKeyboardInView = ^(UIView *view) {
        for (UIView *subview in view.subviews)
        {
            
            Class UIInputSetHostView = NSClassFromString(@"UIInputSetHostView");
            Class UIInputSetContainerView = NSClassFromString(@"UIInputSetContainerView");
            if ([subview isKindOfClass:UIInputSetHostView] && ![subview isKindOfClass:UIInputSetContainerView])
            {
                keyboardView = subview;
            }
            findKeyboardInView(subview);
        }
    };
    
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in [windows reverseObjectEnumerator])//逆序效率更高，因为键盘总在上方
    {
        Class UITextEffectsWindow = NSClassFromString(@"UITextEffectsWindow");
        Class UIRemoteKeyboardWindow = NSClassFromString(@"UIRemoteKeyboardWindow");
        if ([window isKindOfClass:UITextEffectsWindow] && ![window isKindOfClass:UIRemoteKeyboardWindow]) {
            findKeyboardInView(window);
        }
    }
    return keyboardView;
}

- (NSArray<UIView *> *)findSubviewWithClass:(NSString *)classString
{
    __block UIView *tagView;
    __block NSMutableArray<UIView *> *tagSubviews = [NSMutableArray array];
    static void (^findLastViewInView)(UIView *);
    findLastViewInView = ^(UIView *view) {
        for (UIView *subview in [view.subviews reverseObjectEnumerator])
        {
            Class lastClass = NSClassFromString(classString);
            if ([subview isKindOfClass:lastClass])
            {
                tagView = subview;
                [tagSubviews addObject:tagView];
            }
            findLastViewInView(subview);
        }
    };
    findLastViewInView(self);
    return tagSubviews;
}

- (NSArray <__kindof UIView *>*)getSubviews
{
    __block NSMutableArray<UIView *> *subviews = [NSMutableArray array];
    static void (^logSubviews)(UIView *);
    logSubviews = ^(UIView *view) {
        for (UIView *subview in view.subviews) {
            [subviews addObject:subview];
            logSubviews(subview);
        }
    };
    logSubviews(self);

    return subviews;
}

@end
